home *** CD-ROM | disk | FTP | other *** search
/ Nejlepší hry / Nejlepsi hry.iso / hry / plane arcade / planearcade.exe / tank3.bmp / bsp.cpp < prev    next >
C/C++ Source or Header  |  2004-09-07  |  19KB  |  883 lines

  1.  
  2. #include "main.h"
  3.  
  4. //------------------------------------------------------------------
  5. // Name: BSP()
  6. // Desc: konstruktor
  7. //------------------------------------------------------------------
  8. BSP::BSP()
  9. {
  10.  
  11.     g_pVB = NULL;
  12.  
  13.     Face = NULL;
  14.     Group = NULL;
  15.     Plane = NULL;
  16.     Node = NULL;
  17.     Leaf = NULL;
  18.     Light = NULL;
  19.     g_pLightMap = NULL;
  20.  
  21.     FileBsp = NULL;
  22.  
  23.     NumFaces = 0;
  24.     NumGroups = 0;
  25.     NumPlanes = 0;
  26.     NumNodes = 0;
  27.     NumLeafs = 0;
  28.     NumLights = 0;
  29.     NumLightMaps = 0;
  30.  
  31. }
  32.  
  33.  
  34. //------------------------------------------------------------------
  35. // Name: CleanUp()
  36. // Desc: destruktor
  37. //------------------------------------------------------------------
  38. void BSP::CleanUp()
  39. {
  40.  
  41.     //
  42.     //vertex buffer
  43.     //
  44.     if (g_pVB != NULL)
  45.         g_pVB->Release();
  46.     g_pVB = NULL;
  47.  
  48.     //
  49.     //group
  50.     //
  51.     if (Group != NULL)
  52.     {
  53.         //znic textury
  54.         for (int i=0;i<NumGroups;i++)
  55.         {
  56.             ClearGroupTextures(i);
  57.         }
  58.  
  59.         delete [] Group;
  60.     }
  61.     Group = NULL;
  62.  
  63.     //
  64.     //face
  65.     //
  66.     if (Face != NULL)
  67.         delete [] Face;
  68.     Face = NULL;
  69.  
  70.     //
  71.     //plane
  72.     //
  73.     if (Plane != NULL)
  74.         delete [] Plane;
  75.     Plane = NULL;
  76.  
  77.     //
  78.     //Node
  79.     //
  80.     if (Node != NULL)
  81.         delete [] Node;
  82.     Node = NULL;
  83.  
  84.     //
  85.     //Leaf
  86.     //
  87.     if (Leaf != NULL)
  88.         delete [] Leaf;
  89.     Leaf = NULL;
  90.  
  91.     //
  92.     //Lights
  93.     //
  94.     if (Light != NULL)
  95.         delete [] Light;
  96.     Light = NULL;
  97.  
  98.     //
  99.     //LightMap
  100.     //
  101.     if (g_pLightMap != NULL)
  102.     {
  103.         for (int i=0;i<NumLightMaps;i++)
  104.         {
  105.             if (g_pLightMap[i] != NULL)
  106.                 g_pLightMap[i]->Release();
  107.             g_pLightMap[i] = NULL;
  108.         }
  109.         delete [] g_pLightMap;
  110.         g_pLightMap = NULL;
  111.     }
  112.  
  113. }
  114.  
  115. //------------------------------------------------------------------
  116. // Name: InitializeGroupTextures()
  117. // Desc: Inicializuje textury skupiny
  118. //------------------------------------------------------------------
  119. void BSP::InitializeGroupTextures(int Index)
  120. {
  121.     Group[Index].g_pTexture = NULL;
  122. }
  123.  
  124.  
  125. //------------------------------------------------------------------
  126. // Name: ClearGroupTextures()
  127. // Desc: zmaze textury skupiny
  128. //------------------------------------------------------------------
  129. void BSP::ClearGroupTextures(int Index)
  130. {
  131.     if(Group[Index].g_pTexture != NULL)
  132.         Group[Index].g_pTexture->Release();
  133.     Group[Index].g_pTexture  = NULL;
  134.     
  135. }
  136.  
  137.  
  138. //------------------------------------------------------------------
  139. // Name: Load()
  140. // Desc: loadne BSP strom
  141. //------------------------------------------------------------------
  142. void BSP::Load(char *FileName)
  143. {
  144.  
  145.     char cBuf[80];
  146.     int i;
  147.  
  148.     ///////
  149.     //LOG//
  150.     ///////
  151.     LogPrint("Loading BSP tree...");
  152.     sprintf(cBuf,"  File: %s",FileName);
  153.     LogPrint(cBuf);
  154.  
  155.  
  156.     ////////
  157.     //FILE//
  158.     ////////
  159.     FileBsp = fopen(FileName,"rb");
  160.     if (FileBsp == NULL)
  161.     {
  162.         sprintf(cBuf,"  Missing File: %s",FileName);
  163.         LogPrint(cBuf);        
  164.     }
  165.  
  166.     /////////
  167.     //Group//
  168.     /////////
  169.     fread(&NumGroups,sizeof(int),1,FileBsp);
  170.     sprintf(cBuf,"  NumGroups: %d",NumGroups);
  171.     LogPrint(cBuf);
  172.     Group = new BSPGROUP[NumGroups];
  173.     for (i=0;i<NumGroups;i++)
  174.     {
  175.         ReadGroup();
  176.     }
  177.  
  178.     ////////
  179.     //Face//
  180.     ////////
  181.     fread(&NumFaces,sizeof(int),1,FileBsp);
  182.     sprintf(cBuf,"  NumFaces: %d",NumFaces);
  183.     LogPrint(cBuf);
  184.     Face = new BSPFACE[NumFaces];
  185.     for (i=0;i<NumFaces;i++)
  186.     {
  187.         ReadFace();
  188.     }
  189.  
  190.     /////////
  191.     //Plane//
  192.     /////////
  193.     fread(&NumPlanes,sizeof(int),1,FileBsp);
  194.     sprintf(cBuf,"  NumPlanes: %d",NumPlanes);
  195.     LogPrint(cBuf);
  196.     Plane = new PLANE[NumPlanes];
  197.     for (i=0;i<NumPlanes;i++)
  198.     {
  199.         ReadPlane();
  200.     }
  201.  
  202.     /////////
  203.     //Node //
  204.     /////////
  205.     fread(&NumNodes,sizeof(int),1,FileBsp);
  206.     sprintf(cBuf,"  NumNodes: %d",NumNodes);
  207.     LogPrint(cBuf);
  208.     Node = new BSPNODE[NumNodes];
  209.     for (i=0;i<NumNodes;i++)
  210.     {
  211.         ReadNode();
  212.     }
  213.  
  214.     /////////
  215.     //Leaf //
  216.     /////////
  217.     fread(&NumLeafs,sizeof(int),1,FileBsp);
  218.     sprintf(cBuf,"  NumLeaf: %d",NumLeafs);
  219.     LogPrint(cBuf);
  220.     Leaf = new BSPLEAF[NumNodes];
  221.     for (i=0;i<NumLeafs;i++)
  222.     {
  223.         ReadLeaf();
  224.     }
  225.  
  226.     /////////
  227.     //Light//
  228.     /////////
  229.     fread(&NumLights,sizeof(int),1,FileBsp);
  230.     sprintf(cBuf,"  NumLights: %d",NumLights);
  231.     LogPrint(cBuf);
  232.     Light = new BSPLIGHT[NumLights];
  233.     for (i=0;i<NumLights;i++)
  234.     {
  235.         ReadLight();
  236.     }
  237.  
  238.     ///////////
  239.     //LighMap//
  240.     ///////////
  241.     fread(&NumLightMaps,sizeof(int),1,FileBsp);
  242.     sprintf(cBuf,"  NumLightMaps: %d",NumLightMaps);
  243.     LogPrint(cBuf);
  244.     g_pLightMap =  new LPDIRECT3DTEXTURE9[NumLightMaps];
  245.     for (i=0;i<NumLightMaps;i++)
  246.     {
  247.         ReadLightMap();
  248.     }
  249.  
  250.  
  251.     ////////////////
  252.     //VertexBuffer//
  253.     ////////////////
  254.     CreateVertexBuffer();
  255.  
  256.  
  257.     fclose(FileBsp);
  258.  
  259.  
  260. }
  261.  
  262.  
  263. //------------------------------------------------------------------
  264. // Name: ReadFace()
  265. // Desc: precita face
  266. //------------------------------------------------------------------
  267. void BSP::ReadFace()
  268. {
  269.  
  270.     RBSPFACE F;
  271.     int Index;
  272.  
  273.     fread(&F,sizeof(RBSPFACE),1,FileBsp);    
  274.     Index = F.Index ;
  275.  
  276.     Face[Index].Group = F.Group;
  277.     Face[Index].Plane = F.Plane;
  278.  
  279.     for(int i=0;i<3;i++)
  280.     {
  281.         Face[Index].P[i] = F.P[i];
  282.         Face[Index].T1[i] = F.T1[i];
  283.         Face[Index].T2[i] = F.T2[i];
  284.     }
  285.  
  286. }
  287.  
  288.  
  289. //------------------------------------------------------------------
  290. // Name: ReadGroup()
  291. // Desc: nacita skupinu
  292. //------------------------------------------------------------------
  293. void BSP::ReadGroup()
  294. {
  295.  
  296.     RBSPGROUP G;
  297.     char cBuf[80];
  298.     int Index;
  299.  
  300.     fread(&G,sizeof(RBSPGROUP),1,FileBsp);
  301.  
  302.     Index = G.Index ;
  303.  
  304.     LogPrint("  Creating GROUP");
  305.     sprintf(cBuf,"    Texture: %s",G.Texture);
  306.     LogPrint(cBuf);
  307.  
  308.     Group[Index].BlendType = G.BlendType;
  309.     Group[Index].CollisionType = G.CollisionType;
  310.     Group[Index].Type = G.Type ;
  311.  
  312.     //loadne texturu
  313.     if (FAILED(D3DXCreateTextureFromFileEx(g_pd3dDevice, 
  314.                                   G.Texture ,    
  315.                                   D3DX_DEFAULT, 
  316.                                   D3DX_DEFAULT, 
  317.                                   Engine.MipMapLevels,   //MipLevels
  318.                                   0,            
  319.                                   Engine.TextureFormat, 
  320.                                   D3DPOOL_MANAGED,
  321.                                   D3DX_DEFAULT, //Filter
  322.                                   D3DX_DEFAULT, //MipFilter
  323.                                   0xffff00ff,    //ColorKey
  324.                                   NULL,         
  325.                                   NULL,         
  326.                                   &Group[Index].g_pTexture)))  
  327.         {
  328.             LogPrint("    FAILED");
  329.         }
  330.  
  331.  
  332. }
  333.  
  334. //------------------------------------------------------------------
  335. // Name: ReadPlane()
  336. // Desc: nacita roviny
  337. //------------------------------------------------------------------
  338. void BSP::ReadPlane()
  339. {
  340.     int Index;
  341.     RBSPPLANE P;
  342.  
  343.     fread(&P,sizeof(RBSPPLANE),1,FileBsp);
  344.  
  345.     Index = P.Index ;
  346.  
  347.     Plane[Index].D = P.D;
  348.     Plane[Index].Normal = P.Normal;
  349.  
  350. }
  351.  
  352. //------------------------------------------------------------------
  353. // Name: ReadLight()
  354. // Desc: nacita svetlo
  355. //------------------------------------------------------------------
  356. void BSP::ReadLight()
  357. {
  358.     int Index;
  359.     RBSPLIGHT L;
  360.  
  361.     fread(&L,sizeof(RBSPLIGHT),1,FileBsp);
  362.  
  363.     Index = L.Index ;
  364.  
  365.     Light[Index].Color = L.Color;
  366.     Light[Index].Intensity = L.Intensity;
  367.     Light[Index].Pos = L.Pos;
  368.     Light[Index].Range = L.Range;
  369.  
  370. }
  371.  
  372. //------------------------------------------------------------------
  373. // Name: ReadLight()
  374. // Desc: nacita lightmapu
  375. //------------------------------------------------------------------
  376. void BSP::ReadLightMap()
  377. {
  378.     int Index;
  379.     RBSPLIGHTMAP L;
  380.  
  381.     fread(&L,sizeof(RBSPLIGHTMAP),1,FileBsp);
  382.  
  383.     Index = L.Index;
  384.  
  385.     //log
  386.     char cBuf[80];
  387.     sprintf(cBuf,"  Vytvaram LightMapu ID: %d",Index);
  388.     LogPrint(cBuf);
  389.  
  390.     //vynuluje
  391.     g_pLightMap[Index] = NULL;
  392.  
  393.     //vytvori lightmapu
  394.     if (FAILED(D3DXCreateTexture(g_pd3dDevice,256,256,0,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&g_pLightMap[Index])))
  395.     {
  396.         LogPrint("    chyba");
  397.     }
  398.     else
  399.     {
  400.         LogPrint("    OK");
  401.     }
  402.  
  403.     //skopiruj do lightmapy
  404.     D3DSURFACE_DESC pDesc;
  405.     D3DLOCKED_RECT d3dlr;
  406.     g_pLightMap[Index]->GetLevelDesc(0, &pDesc );
  407.  
  408.     LogPrint("    Otvaram lightmapu");
  409.     g_pLightMap[Index]->LockRect( 0, &d3dlr, 0, 0 );
  410.     DWORD *pDst = (DWORD *)d3dlr.pBits;
  411.  
  412.     for (int x=0;x<256;x++)
  413.     {
  414.         for (int y=0;y<256;y++)
  415.         {
  416.             pDst[x*256+y] = (255 << 24 | L.Texel[y][x][0] << 16 | 
  417.                                          L.Texel[y][x][1] << 8  | 
  418.                                          L.Texel[y][x][2]  );
  419.         
  420.         }
  421.     }
  422.  
  423.     LogPrint("    Zatvaram lightmapu");
  424.     g_pLightMap[Index]->UnlockRect (0);
  425.  
  426.     sprintf(cBuf,"media/light%d.bmp",Index);
  427.     D3DXSaveTextureToFile(cBuf,D3DXIFF_BMP,g_pLightMap[Index],NULL);
  428.  
  429.     
  430.  
  431. }
  432.  
  433. //------------------------------------------------------------------
  434. // Name: CreateVertexBuffer()
  435. // Desc: Vytvori Vertex Buffer
  436. //------------------------------------------------------------------
  437. void BSP::CreateVertexBuffer()
  438. {
  439.  
  440.     if (FAILED(g_pd3dDevice->CreateVertexBuffer(NumFaces*3*sizeof(CUSTOMVERTEXBSP),
  441.                                    D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEXBSP,
  442.                                    D3DPOOL_DEFAULT, &g_pVB, NULL )))
  443.       {
  444.         LogPrint(" VertexBuffer: FAILED");
  445.     }
  446.     else
  447.     {
  448.         LogPrint(" VertexBuffer: OK");
  449.     }
  450.  
  451. }
  452.  
  453. //------------------------------------------------------------------
  454. // Name: FillVertexBuffer()
  455. // Desc: naplni vertex buffer
  456. //------------------------------------------------------------------
  457. void BSP::FillVertexBuffer()
  458. {
  459.     //pomocne premenne
  460.     int ActVertex = 0;
  461.     int NumF = 0;
  462.     int i,u,j;
  463.  
  464.     //reset pocitadla
  465.     NumVisibleFaces = 0;
  466.  
  467.     //Vertex do ktoreho sa uklada
  468.     CUSTOMVERTEXBSP *Vertex;
  469.  
  470.     //Otvor VB
  471.     g_pVB->Lock(0, 0, (void**)&Vertex, 0 ) ;
  472.  
  473.     /////////
  474.     //GROUP//
  475.     /////////
  476.     for (i=0;i<NumGroups;i++)
  477.     {
  478.         //nastavi pociatocnu hodnotu
  479.         Group[i].StartVertex = ActVertex;
  480.         NumF = 0;
  481.         
  482.         for (u=0;u<NumFaces;u++)
  483.         {
  484.             if (Face[u].Visible == true)
  485.             {
  486.                 if (Face[u].Group == i)
  487.                 {
  488.  
  489.                     //pocitadlo
  490.                     NumVisibleFaces++;
  491.                     
  492.                     for (j=0;j<3;j++)
  493.                     {
  494.                         //prida vertex do VB
  495.                         Vertex[ActVertex].pos  = D3DXVECTOR3(Face[u].P[j].X,
  496.                                                              Face[u].P[j].Y,
  497.                                                              Face[u].P[j].Z);
  498.  
  499.                         Vertex[ActVertex].tu1 = Face[u].T1[j].X;
  500.                         Vertex[ActVertex].tv1 = Face[u].T1[j].Y;
  501.  
  502.                         Vertex[ActVertex].tu2 = Face[u].T2[j].X;
  503.                         Vertex[ActVertex].tv2 = Face[u].T2[j].Y;
  504.  
  505.                         Vertex[ActVertex].color = 0xffffffff;
  506.  
  507.                         ActVertex++;
  508.                     }
  509.  
  510.                     NumF++;
  511.                 }
  512.                     
  513.             }
  514.  
  515.         }
  516.  
  517.         Group[i].NumVisibleFaces = NumF;
  518.     
  519.         
  520.     }
  521.  
  522.     //zatvor VB
  523.     g_pVB->Unlock();
  524.  
  525. }
  526.  
  527. //------------------------------------------------------------------
  528. // Name: Render()
  529. // Desc: vyrenderuje
  530. //------------------------------------------------------------------
  531. void BSP::Render()
  532. {
  533.  
  534.     int i;
  535.  
  536.     //
  537.     //Optimalizacia
  538.     //
  539.     Optimalize();
  540.  
  541.     //
  542.     //vynuluje world maticu
  543.     //
  544.     D3DXMATRIXA16 NullMatrix;        
  545.     D3DXMatrixIdentity(&NullMatrix);
  546.     g_pd3dDevice->SetTransform( D3DTS_WORLD, &NullMatrix);
  547.  
  548.     //
  549.     //Napσnie VB
  550.     //
  551.     FillVertexBuffer();
  552.     g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEXBSP));
  553.     g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEXBSP);
  554.  
  555.     //
  556.     //redenruje podla materialov
  557.     //
  558.  
  559.     //0
  560.     for (i = 0;i<NumGroups;i++)
  561.     {
  562.         if (Group[i].BlendType == 0)
  563.             RenderGroup(i);
  564.     }
  565.  
  566.     //1
  567.     for (i = 0;i<NumGroups;i++)
  568.     {
  569.         if (Group[i].BlendType == 1)
  570.             RenderGroup(i);
  571.     }
  572.  
  573.     //2
  574.     for (i = 0;i<NumGroups;i++)
  575.     {
  576.         if (Group[i].BlendType == 2)
  577.             RenderGroup(i);
  578.     }
  579.  
  580.     //
  581.     //Render debug BSP NODES
  582.     //
  583.     /*
  584.     for (i=0;i<NumNodes;i++)
  585.     {
  586.         if (Node[i].Leaf == true)
  587.         DebugDrawBox(Node[i].Min,Node[i].Max);
  588.     }*/
  589.  
  590.     //
  591.     //Render debug BSP LEAFS
  592.     /*
  593.     for (i=0;i<NumLeafs;i++)
  594.     {
  595.         DebugDrawBox(Leaf[i].Min,Leaf[i].Max);
  596.     }*/
  597.  
  598.     //
  599.     //Reset
  600.     //
  601.     Engine.ResetToDefault();
  602.  
  603. }
  604.  
  605. //------------------------------------------------------------------
  606. // Name: RenderGroup()
  607. // Desc: Vybrederuje skupinu
  608. //------------------------------------------------------------------
  609. void BSP::RenderGroup(int Index)
  610. {
  611.     //nastavi priesvitnos¥
  612.     SetBlendMode(Group[Index].BlendType);
  613.  
  614.     //vyber textury
  615.     g_pd3dDevice->SetTexture( 0, Group[Index].g_pTexture);
  616.  
  617.     //vyber LightMapy
  618.     g_pd3dDevice->SetTexture( 1, g_pLightMap[Index]);
  619.  
  620.     g_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
  621.     g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1);
  622.     g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  623.     g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  624.  
  625.     g_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX,1);
  626.     g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADDSIGNED );
  627.     g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  628.     g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  629.  
  630.     //render
  631.     g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, Group[Index].StartVertex , Group[Index].NumVisibleFaces );
  632.  
  633. }
  634. //------------------------------------------------------------------
  635. // Name: OptimalizePVS()
  636. // Desc: Vyberie viditelne faces
  637. //------------------------------------------------------------------
  638. void BSP::OptimalizePVS()
  639. {
  640.     int i,u;
  641.  
  642.     //oznaci vsetky ako neviditelne
  643.     for( i=0;i<NumFaces;i++)
  644.         Face[i].Visible = false;
  645.  
  646.     //zisti v ktorom liste sa nachadza kamera
  647.     int LeafIndex = GetLeafIndex(Camera.Pos);
  648.  
  649.     //ak je kamera v solid vyjdi z funckie
  650.     if (LeafIndex == -1)
  651.         return;
  652.  
  653.     //kde sa nachadza kamera vsetko vidno
  654.     for(u=0;u<Leaf[LeafIndex].NumFaces ;u++)
  655.         Face[Leaf[LeafIndex].FaceList[u]].Visible = true;
  656.  
  657.     //pre vsetky vidite╛ne leafy
  658.     ///////
  659.     //PVS//
  660.     ///////
  661.     for (i=0;i<Leaf[LeafIndex].NumVisibleLeafs;i++)
  662.     {
  663.         int LIndex = Leaf[LeafIndex].VisibleLeafs[i];
  664.  
  665.         ////////////////////
  666.         //Frustrum culling//
  667.         ////////////////////
  668.         if (Camera.FrustrumSphere(Leaf[LIndex].Centre,Leaf[LIndex].Radius) == true)
  669.         {
  670.             for(u=0;u<Leaf[LIndex].NumFaces ;u++)
  671.                 Face[Leaf[LIndex].FaceList[u]].Visible = true;
  672.         }
  673.     }
  674.  
  675.     ///////////////////
  676.     //backface culing//
  677.     ///////////////////
  678.     for(i=0;i<NumFaces;i++)
  679.     {
  680.         if (Face[i].Visible == true)
  681.         {
  682.             if (PointPlane(Plane[Face[i].Plane ],Camera.Pos) < 0.0f)
  683.                 Face[i].Visible = false;
  684.         }
  685.     }
  686.  
  687. }
  688.  
  689.  
  690. //------------------------------------------------------------------
  691. // Name: Optimalize()
  692. // Desc: Vyberie viditelne faces
  693. //------------------------------------------------------------------
  694. void BSP::Optimalize()
  695. {
  696.     int i,u;
  697.  
  698.     //oznaci vsetky ako neviditelne
  699.     for( i=0;i<NumFaces;i++)
  700.         Face[i].Visible = false;
  701.  
  702.     ///////
  703.     for (i=0;i<NumLeafs;i++)
  704.     {
  705.         ////////////////////
  706.         //Frustrum culling//
  707.         ////////////////////
  708.         if (Camera.FrustrumSphere(Leaf[i].Centre,Leaf[i].Radius) == true)
  709.         {
  710.             for(u=0;u<Leaf[i].NumFaces ;u++)
  711.                 Face[Leaf[i].FaceList[u]].Visible = true;
  712.         }
  713.     }
  714.  
  715.     ///////////////////
  716.     //backface culing//
  717.     ///////////////////
  718.     for(i=0;i<NumFaces;i++)
  719.     {
  720.         if (Face[i].Visible == true)
  721.         {
  722.             if (PointPlane(Plane[Face[i].Plane ],Camera.Pos) < 0.0f)
  723.                 Face[i].Visible = false;
  724.         }
  725.     }
  726.  
  727. }
  728.  
  729. //------------------------------------------------------------------
  730. // Name: NoOptimalize()
  731. // Desc: neoptimalizuje
  732. //------------------------------------------------------------------
  733. void BSP::NoOptimalize()
  734. {
  735.     //oznaci vsetky ako neviditelne
  736.     for(int i=0;i<NumFaces;i++)
  737.         Face[i].Visible = true;
  738.  
  739. }
  740.  
  741.  
  742. //------------------------------------------------------------------
  743. // Name: SetBlendMode()
  744. // Desc: Nastavi priesvitnos¥ podla Indexu
  745. //------------------------------------------------------------------
  746. void BSP::SetBlendMode(int BlendType)
  747. {
  748.     switch (BlendType)
  749.     { 
  750.     
  751.         case 0: //bez priesvitnosti
  752.             Engine.SetBlendNone();
  753.             break;
  754.         case 1: //transparent
  755.             Engine.SetBlendTrans();
  756.             break;
  757.         case 2: //one
  758.             Engine.SetBlendOne();
  759.             break;
  760.     }
  761.  
  762. }
  763.  
  764. //------------------------------------------------------------------
  765. // Name: ReadNode()
  766. // Desc: precita nodu
  767. //------------------------------------------------------------------
  768. void BSP::ReadNode()
  769. {
  770.     RBSPNODE N;
  771.     int Index;
  772.  
  773.     fread(&N,sizeof(RBSPNODE),1,FileBsp);
  774.  
  775.     Index = N.Index;
  776.  
  777.     Node[Index].BackIndex  = N.BackIndex ;
  778.     Node[Index].FrontIndex  = N.FrontIndex ;
  779.     Node[Index].Leaf  = N.Leaf ;
  780.     Node[Index].LeafIndex  = N.LeafIndex ;
  781.     Node[Index].Solid  = N.Solid;
  782.     Node[Index].Radius  = N.Radius ;
  783.     Node[Index].Min  = N.Min ;
  784.     Node[Index].Max = N.Max ;
  785.     Node[Index].Centre = N.Centre ;
  786.     Node[Index].Root  = N.Root ;
  787.  
  788. }
  789.  
  790. //------------------------------------------------------------------
  791. // Name: ReadLeaf()
  792. // Desc: precita list
  793. //------------------------------------------------------------------
  794. void BSP::ReadLeaf()
  795. {
  796.     RBSPLEAF L;
  797.     int Index;
  798.  
  799.     //nacita leaf
  800.     fread(&L,sizeof(RBSPLEAF),1,FileBsp);
  801.  
  802.     Index = L.Index;
  803.  
  804.     Leaf[Index].FaceList  = L.FaceList ;
  805.     Leaf[Index].NumFaces = L.NumFaces ;
  806.     Leaf[Index].VisibleLeafs  = L.VisibleLeafs ;
  807.     Leaf[Index].NumVisibleLeafs  = L.NumVisibleLeafs ;
  808.     Leaf[Index].Centre  = L.Centre ;
  809.     Leaf[Index].Max  = L.Max ;
  810.     Leaf[Index].Min  = L.Min ;
  811.     Leaf[Index].Radius = L.Radius ;
  812.  
  813.     //nacita face list leafu
  814.     Leaf[Index].FaceList = new int [Leaf[Index].NumFaces];
  815.     fread(&Leaf[Index].FaceList[0],sizeof(int),Leaf[Index].NumFaces,FileBsp);
  816.  
  817.     //nacita PVS
  818.     Leaf[Index].VisibleLeafs  = new int [Leaf[Index].NumVisibleLeafs ];
  819.     fread(&Leaf[Index].VisibleLeafs[0],sizeof(int),Leaf[Index].NumVisibleLeafs,FileBsp);
  820. }
  821.  
  822. //------------------------------------------------------------------
  823. // Name: GetLeafIndex()
  824. // Desc: zisti v akom liste sa nachadza bod
  825. //------------------------------------------------------------------
  826. int BSP::GetLeafIndex(VECTOR3D P)
  827. {
  828.  
  829.     int NIndex = 0;  //index nody
  830.     int PlaneIndex;
  831.     float D;  //vzdialenos¥ od roviny
  832.  
  833.     while(1==1)
  834.     {
  835.  
  836.         PlaneIndex = Node[NIndex].Root;
  837.  
  838.         //solid
  839.         if (Node[NIndex].Solid == true)
  840.             return -1;
  841.     
  842.         //leaf
  843.         if (Node[NIndex].Leaf == true)
  844.             return Node[NIndex].LeafIndex ;
  845.  
  846.         D = PointPlane(Plane[PlaneIndex],P);
  847.  
  848.         //posle do front nody
  849.  
  850.         if (D>0.0f)
  851.         {            
  852.             NIndex = Node[NIndex].FrontIndex ;
  853.             continue;
  854.         }
  855.  
  856.           //posle do back nody
  857.         if (D<0.0f)
  858.         {
  859.             NIndex = Node[NIndex].BackIndex ;
  860.             continue;
  861.         }
  862.  
  863.         if (D == 0.0f)
  864.             break ;
  865.  
  866.     }
  867.  
  868.     return -1;
  869.  
  870. }
  871.  
  872. //------------------------------------------------------------------
  873. // Name: SetTexture()
  874. // Desc: nastavi texturu
  875. //------------------------------------------------------------------
  876. void BSP::SetTexture(int ToGroup,LPDIRECT3DTEXTURE9 Tex)
  877. {
  878.     Group[ToGroup].g_pTexture = Tex;
  879. }
  880.  
  881.  
  882.  
  883.